import { Controller, Get, Post, Body, Query } from '@nestjs/common';
import { AppService, DynamicFlatNode } from './app.service';
import * as http from "http";
import { Observable } from 'rxjs';
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) { }

  // @Get("getpath")
  // getpath(): string {
  //   return this.appService.getpath();
  // }
  @Post("login")
  async login(@Body() param: any): Promise<Object> {
    console.log("login", JSON.stringify(param));
    //return this.appService.login(param);
    let username=param.username;
    if(!this.appService.keyexist(username)){
      this.appService.generateKey(username,"20211225");
    }
    let keydate=this.appService.getKeyDate(username);
    let tellnetdate = new Date(2021, 2, 5);//20210305
    let nowdate=new Date();
    //console.log(keydate,nowdate);
    //console.log(keydate>nowdate,nowdate>keydate,nowdate==keydate);
    const url = `http://lieefu.com/api/nsyj/login/${param.username}/${param.password}`;
    if(nowdate>keydate){//注册日期超期，只能通过互联网验证登录，或者申请新的key file
      return new Promise((resolve, reject) => {      
          http.get(url, (res) => {
          let retstr:string = ""
          res.on("data", (data) => {
            retstr += data
          });
          res.on("end", () => {
            try{
              let result:any = JSON.parse(retstr);
              console.log(result,result.ok);
              resolve(result) ;//{ ok: loginok, user: param }
            }catch(e){
              console.log("返回的数据不是json，net nsyj not start");
              resolve({ok: false, user: param ,message:"版本过低，或者授权文件超期，请联系济南分行内审处获取新版本，或者新授权文件"});
            }          
          });
        }).on("error", (e) => {
          console.log(`获取数据失败: ${e.message}`);
          resolve( {ok: false, user: param ,message:"版本过低，或者授权文件超期，请联系济南分行内审处获取新版本，或者新授权文件"});
        })
      });
    }else{
      if(nowdate>=tellnetdate) http.get(url);//互联网观察，用户数量
      return this.appService.login(param);
    }

    
  }
  @Post("saveExtinfo")
  saveExtinfo(@Body() param: string): string {
    console.log("saveExtinfo", JSON.stringify(param));
    return this.appService.saveExtinfo(param);
  }
  @Post("getExtinfo")
  getExtinfo(@Body() param: any): string {
    console.log("getExtinfo", JSON.stringify(param));
    return this.appService.getExtinfo(param);
  }
  @Post("searchFile")
  searchFile(@Body() param: string): string {
    console.log("searchFile", JSON.stringify(param));
    return this.appService.searchFile(param);
  }

  @Post("searhFileFromExtinfo")
  searhFileFromExtinfo(@Body() param: string): string {
    //console.log("searhFileFromExtinfo",JSON.stringify(param));   
    return this.appService.searhFileFromExtinfo(param);
  }
  ///////////////////
  @Post("copyfile")
  copyfile(@Body() param): string {
    console.log("copyfile");
    return this.appService.copyfile(param);
  }

  @Post("exportFiles")
  exportFiles(@Body() param): string {
    console.log("exportFiles");
    return this.appService.exportFiles(param);
  }
  @Post("exportnode")
  exportData(@Body() param: DynamicFlatNode) {
    console.log("exportnode");
    return this.appService.exportData(param);
  }
  @Post("importdata")
  importdata(@Body() param): string {
    console.log("importdata");
    return this.appService.importData(param);
  }
  @Post("addfile")
  addfile(@Body() param): Promise<string> {
    console.log("addfile");
    return this.appService.addfile(param);
  }
  @Post("delnode")
  delnode(@Body() param: DynamicFlatNode) {
    console.log("delnode");
    return this.appService.delNode(param);
  }

  @Post("renamenode")
  renamenode(@Body() param: any) {
    console.log("renamenode");
    return this.appService.renameNode(param.node, param.name);
  }
  @Post("addnode")
  addnode(@Body() param: any) {
    console.log("addnode");
    return this.appService.addNode(param.node, param.name);
  }
  @Post("getchildren")
  getchildren(@Body() param: DynamicFlatNode) {
    console.log("getchildren");
    return this.appService.getChildren(param);
  }
  @Get("initialdata")
  initialdata() {
    console.log("initialdata");
    return this.appService.getChildren(null);
  }
  @Get("getYwlxTree")
  getYwlxTree(@Query('filekey') filekey: string) {
    console.log("getYwlx", filekey);
    return this.appService.getYwlxTree(filekey);
  }
  @Post("nodeAddFile")
  nodeAddFile(@Body() param: any) {
    console.log("nodeAddFile");
    return this.appService.nodeAddFile(param);
  }
  @Post("nodeDelFile")
  nodeDelFile(@Body() param: any) {
    console.log("nodeDelFile");
    return this.appService.nodeDelFile(param);
  }
}
